feat: improve setItem performance on iOS #16
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📜 Description
In this update, I replaced the original approach of attempting an item add followed by a conditional update in case of failure with a simplified approach that deletes the existing item and then adds the new one. This refactor applies to secure storage management, specifically replacing the second code snippet (conditional logic) with the first code snippet (delete-then-add).
Before: The code attempted to add an item using SecItemAdd(). If it failed due to the item already existing (e.g., status != errSecSuccess), it fell back to SecItemUpdate() to modify the existing item. This involved creating an additional updateDictionary and invoking SecItemUpdate(), increasing complexity and overhead.
After: The code now first deletes any existing item using SecItemDelete() and then unconditionally adds the new item using SecItemAdd(). This avoids the need for conditional checks and extra logic for updates.
Rationale for the Change:
Performance Optimization:
The previous code path had to handle two cases: adding a new item and updating an existing item. This resulted in multiple system calls (one for SecItemAdd() and one for SecItemUpdate() in case of failure), adding both complexity and time overhead.
By simplifying the flow to always delete the existing item first, we eliminate the need for conditionally updating the item. This reduces the number of operations and the need for additional dictionary manipulations.
Expected Performance Improvement: Fewer conditional checks and system calls should lead to faster execution, especially when items frequently exist and require updates.
Simplified Logic:
The new approach is more straightforward: instead of handling two separate cases (add vs. update), the logic always deletes and then adds the item. This reduces the cognitive load for anyone maintaining the code in the future, as the flow is linear and predictable.
Reduced Complexity:
In the original approach, failure of SecItemAdd() led to a fallback that required creating an updateDictionary and making a separate SecItemUpdate() call. This additional branching and dictionary creation added complexity and increased the risk of mistakes or maintenance issues.
The new approach avoids extra dictionary allocations and keeps the code simpler and more maintainable.
💡 Motivation and Context
Performance comprassion
set 1000 items to secure storage
🤔 How Has This Been Tested?
iPhone 15 Pro
📝 Checklist